<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-width="300px">
      <el-form-item :label="$t('订阅可提前天数')" prop="lumiere_subscribe_advance_day">
        <el-input v-model="dataForm.lumiere_subscribe_advance_day"/>
      </el-form-item>
      <el-form-item>
        <el-button @click="cancel">{{$t('Cancel')}}</el-button>
        <el-button type="primary" @click="update">{{$t('Confirm')}}</el-button>
      </el-form-item>
    </el-form></div>
</template>

<script>
import { listExpress, updateExpress } from '@/api/config'

export default {
  name: 'ConfigSubscribe',
  data() {
    return {
      dataForm: {
        lumiere_subscribe_advance_day: 0
      },
      rules: {
        lumiere_subscribe_advance_day: [
          { required: true, message: this.$t('不能为空'), trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init: function() {
      listExpress().then(response => {
        this.dataForm = response.data.data
      })
    },
    cancel() {
      this.init()
    },
    update() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return
        }
        updateExpress(this.dataForm).then(response => {
          this.$notify.success({
            title: this.$t('Success!'),
            message: this.$t('运费配置修改成功')
          })
        }).catch(response => {
          this.$notify.error({
            title: this.$t('Failed'),
            message: this.$t(response.data.errmsg)
          })
        })
      })
    }
  }
}
</script>
